import urllib.request
import urllib.parse
import os
import re

# 处理单个HTML文件的函数
def extract_and_download_pdf(html_file_path):
    print(f"\n处理文件: {html_file_path}")
    
    # 读取HTML文件内容
    try:
        with open(html_file_path, 'r', encoding='utf-8', errors='ignore') as f:
            html_content = f.read()
        print(f"成功读取HTML文件，大小: {len(html_content)} 字节")
    except Exception as e:
        print(f"读取HTML文件失败: {e}")
        return False
    
    # 搜索PDF链接的正则表达式
    pdf_url_pattern = r'file=https%3A%2F%2Fmoor-smile\.shec\.edu\.cn%2Frest%2Fpdf%2F[^&]+'
    
    # 查找PDF链接
    match = re.search(pdf_url_pattern, html_content)
    if match:
        encoded_url = match.group(0).replace('file=', '')
        # URL解码
        pdf_url = encoded_url.replace('%3A', ':').replace('%2F', '/')
        print(f"找到PDF URL: {pdf_url}")
    else:
        print("未找到PDF URL")
        return False
    
    # 定义保存的文件名（使用HTML文件名但替换为.pdf扩展名）
    base_name = os.path.splitext(os.path.basename(html_file_path))[0]
    if not base_name.endswith('.pdf'):
        base_name += '.pdf'
    pdf_save_path = os.path.join(os.path.dirname(html_file_path), base_name)
    
    # 正确编码URL，只编码路径部分
    url_parts = list(urllib.parse.urlparse(pdf_url))
    url_parts[2] = urllib.parse.quote(url_parts[2])  # 编码path部分
    encoded_pdf_url = urllib.parse.urlunparse(url_parts)
    
    # 下载PDF文件
    try:
        print(f"开始下载PDF文件...")
        print(f"编码后URL: {encoded_pdf_url}")
        print(f"保存路径: {pdf_save_path}")
        
        # 创建请求对象，添加用户代理以模拟浏览器请求
        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
        req = urllib.request.Request(encoded_pdf_url, headers=headers)
        
        # 下载文件
        with urllib.request.urlopen(req) as response, open(pdf_save_path, 'wb') as out_file:
            data = response.read()
            out_file.write(data)
        
        # 检查文件是否成功下载
        if os.path.exists(pdf_save_path):
            file_size = os.path.getsize(pdf_save_path) / 1024 / 1024  # 转换为MB
            print(f"PDF文件下载成功！")
            print(f"保存路径: {pdf_save_path}")
            print(f"文件大小: {file_size:.2f} MB")
            return True
        else:
            print("PDF文件下载失败，文件不存在。")
            return False
    except Exception as e:
        print(f"下载PDF文件时发生错误: {e}")
        return False

# 主函数
if __name__ == "__main__":
    # 要处理的HTML文件列表
    html_files = [
        r"d:\D\大学生就业去向影响因素数据集.pdf.html",
        r"d:\D\中国汽车不同车型每月销售量数据集.pdf.html"
    ]
    
    # 处理每个HTML文件
    success_count = 0
    for html_file in html_files:
        if extract_and_download_pdf(html_file):
            success_count += 1
    
    # 输出总结
    print(f"\n处理完成！成功下载 {success_count} 个PDF文件，共 {len(html_files)} 个文件。")